package com.woniu.fly.fairmall.db.service;


import com.woniu.fly.fairmall.db.dao.FairmallPermissionDao;
import com.woniu.fly.fairmall.db.entity.FairmallPermission;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 权限表(FairmallPermission)表服务实现类
 *
 * @author makejava
 * @since 2020-04-02 13:45:03
 */
@Service("fairmallPermissionService")
public class FairmallPermissionService {
    @Resource
    private FairmallPermissionDao fairmallPermissionDao;

    @Resource
    private FairmallRoleService roleService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public FairmallPermission queryById(Integer id) {
        return this.fairmallPermissionDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    public List<FairmallPermission> queryAllByLimit(int offset, int limit) {
        return this.fairmallPermissionDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param fairmallPermission 实例对象
     * @return 实例对象
     */
    public FairmallPermission insert(FairmallPermission fairmallPermission) {
        this.fairmallPermissionDao.insert(fairmallPermission);
        return fairmallPermission;
    }

    /**
     * 修改数据
     *
     * @param fairmallPermission 实例对象
     * @return 实例对象
     */
    public FairmallPermission update(FairmallPermission fairmallPermission) {
        this.fairmallPermissionDao.update(fairmallPermission);
        return this.queryById(fairmallPermission.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public boolean deleteById(Integer id) {
        return this.fairmallPermissionDao.deleteById(id) > 0;
    }

    public Set<String> queryByRoleIds(Integer[] roleIds) {
        Set<String> permissions = new HashSet<>();
        if(roleIds.length == 0){
            return permissions;
        }
        String ids = roleService.getInRoleIds(Arrays.asList(roleIds));
        List<FairmallPermission> permList = fairmallPermissionDao.queryPermListByIds(ids);
        for(FairmallPermission perm : permList){
            permissions.add(perm.getPermission());
        }
        return permissions;
    }

    public boolean checkSuperPermission(Integer roleId) {
        if(roleId == null){
            return false;
        }
        FairmallPermission fairmallPermission = new FairmallPermission();
        fairmallPermission.setRoleId(roleId);
        fairmallPermission.setPermission("*");
        fairmallPermission.setDeleted(false);
        List<FairmallPermission> fairmallPermissions = fairmallPermissionDao.queryAll(fairmallPermission);
        return !CollectionUtils.isEmpty(fairmallPermissions);
    }

    public Set<String> queryByRoleId(Integer roleId) {
        Set<String> permissions = new HashSet<String>();
        if(roleId == null){
            return permissions;
        }
        FairmallPermission fairmallPermission = new FairmallPermission();
        fairmallPermission.setRoleId(roleId);
        fairmallPermission.setDeleted(false);
        List<FairmallPermission> fairmallPermissions = fairmallPermissionDao.queryAll(fairmallPermission);
        for(FairmallPermission permission : fairmallPermissions){
            permissions.add(permission.getPermission());
        }
        return permissions;
    }
}
